package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.dto.RemovalReportDataDto;
import cn.com.cifi.mars.common.enums.LocalCommandEnum;
import cn.com.cifi.mars.common.enums.RedisChannelEnum;
import cn.com.cifi.mars.entity.DmDmRemovalReport;
import cn.com.cifi.mars.mapper.mars.DmDmRemovalReportMapper;
import cn.com.cifi.mars.service.CacheService;
import cn.com.cifi.mars.service.DmDmRemovalReportService;
import cn.com.cifi.mars.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author plx
 * @since 2020-03-30
 */
@Slf4j
@Service
public class DmDmRemovalReportServiceImpl extends ServiceImpl<DmDmRemovalReportMapper, DmDmRemovalReport> implements DmDmRemovalReportService {

    LinkedBlockingQueue<LocalCommandEnum> commandQueue = new LinkedBlockingQueue<>();

    @Autowired
    private CacheService cacheService;

    @Override
    public void sendCommand(LocalCommandEnum command) {
        //保证出现异常不影响原来服务
        try {
            boolean result = commandQueue.offer(command);
            if (!result){
                log.info("returned false");
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }

    @Override
    public LocalCommandEnum takeCommand() throws InterruptedException {
        return commandQueue.take();
    }

    @Override
    public void sync(String endDate) {
        String endTime = DateUtils.toEndTimeStr(endDate);
        String[] split = endDate.split("-");
        baseMapper.insertSync(endDate,endTime,split[0],split[1]);
        String localEndDate = LocalDate.now().format(DateTimeFormatter.ofPattern(DateUtils.DATE_JFP_STR));
        if (localEndDate.equals(endDate)){
            cacheService.pushCommand(RedisChannelEnum.CHANNEL_REMOVAL,LocalCommandEnum.FLUSH_REMOVAL_REPORT_CACHE);
        }
    }

    @Override
    public String getLatestEndDate(String endDate) {
        QueryWrapper<DmDmRemovalReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(end_date) endDate").le("end_date",endDate);
        DmDmRemovalReport one = getOne(queryWrapper);
        if (one == null || one.getEndDate() == null){
            return null;
        }
        return one.getEndDate();
    }

	@Override
	public List<String> getProjectIds(List<String> divisionList, List<String> projectIdList) {
		return baseMapper.getProjectIds(divisionList,projectIdList);
	}

	@Override
	public List<RemovalReportDataDto> getRemovalReportDataList(String latestEndDate, List<String> projectIds,List<String> projectIdList) {
		return baseMapper.selectRemovalReportDataList(latestEndDate,projectIds,projectIdList);
	}

	/**
	 * 处理去化率大表数据，依次按顺序执行
	 */
	@Override
	public void syncRemovalDataJob(String yearMonth) {
		log.info("同步处理去化率大表数据开始，截止日期：{}",yearMonth);
		baseMapper.removeTmp01DmDmValueReport();
		//刷新层级数据
		baseMapper.syncRemovalLevle(yearMonth);
		
		baseMapper.removeTmp02DmDmValueReport();
		//刷新层级关系数据
		baseMapper.syncRemovalLevleRelation(yearMonth);
		
		//刷新楼栋层级基础数据
		baseMapper.removeTmp03DmDmValueReport();
		baseMapper.syncRemovalBuildBase(yearMonth);
		//刷新产品构成层级基础数据
		baseMapper.removeTmp03DmDmValueReport();
		baseMapper.syncRemovalProductBase(yearMonth);
		//刷新业态层级基础数据
		baseMapper.removeTmp03DmDmValueReport();
		baseMapper.syncRemovalProProductBase(yearMonth);
		//刷新组团基础数据
		baseMapper.removeTmp03DmDmValueReport();
		baseMapper.syncRemovalGroupBase(yearMonth);
		//刷新分期基础数据
		baseMapper.removeTmp03DmDmValueReport();
		baseMapper.syncRemovalStageBase(yearMonth);
		//刷新项目基础数据
		baseMapper.removeTmp03DmDmValueReport();
		baseMapper.syncRemovalProjectBase(yearMonth);
		
		baseMapper.removeDmDmValueReportSummary(yearMonth);
		//添加汇总楼栋层级数据
		baseMapper.syncRemovalBuildData(yearMonth);
		//添加汇总产品构成层级数据
		baseMapper.syncRemovalProductData(yearMonth);
		//添加汇总业态层级数据
		baseMapper.syncRemovalProProductData(yearMonth);
		//添加汇总组团层级数据
		baseMapper.syncRemovalGroupData(yearMonth);
		//添加汇总分期层级数据
		baseMapper.syncRemovalStageData(yearMonth);
		//添加汇总项目层级数据
		baseMapper.syncRemovalProjectData(yearMonth);
		log.info("同步处理去化率大表数据结束，截止日期：{}",yearMonth);
	}
}
